<template>
  <div class="search">
<form action="/">
  <van-search
  background="#3296fa"
    v-model="value"
    show-action
    placeholder="请输入搜索关键词"
    @search="onSearch"
    @focus='isShow=false'
    @cancel="$router.back()"
  />
</form>
<search-results :searchText='value' v-if="isShow" />
<search-history @deleteAll=' history = []' @onSearch='onSearch' :history='history' v-else-if="!value" />
<search-suggestion @onSearch='onSearch' :valueText='value' v-else />
  </div>
</template>

<script>
import SearchHistory from './components/SearchHistory.vue'
import SearchResults from './components/SearchResults.vue'
import SearchSuggestion from './components/SearchSuggestion.vue'
import { setStorage, getStorage } from '@/utils/storage.js'
export default {
  components: { SearchHistory, SearchSuggestion, SearchResults },
  name: 'search',
  data () {
    return {
      value: '',
      isShow: false,
      history: getStorage('TT-HISTORY') || [] // 搜索历史
    }
  },

  created () {

  },
  watch: {
    history: {
      deep: true,
      handler (val) {
        setStorage('TT-HISTORY', val)
      }
    }
  },
  methods: {
    onSearch (val) {
      this.value = val
      this.isShow = true
      // 搜索历史
      this.history.unshift(val)
      this.history = [...new Set(this.history)]
    }
  }
}
</script>

<style scoped lang='less'>
.search{
  .van-search__action{
    color: #fff;
  }
}
</style>
